package com.yc.bbs.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.bbs.entity.Topic;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * BBS论坛-帖子表-19 Mapper 接口
 * </p>
 *
 * @author ly
 * @since 2024-11-23
 */
public interface TopicMapper extends BaseMapper<Topic> {

    @Select("SELECT * FROM tbl_topic ORDER BY publishtime DESC LIMIT 5")
    List<Topic> selectLatestTopics();


    @Select("SELECT\n" +
            "	a.*,\n" +
            "	b.cname \"user.cname\",\n" +
            "	b.head \"user.head\"," +
            "	b.regtime \"user.regtime\"," +
            "	reply_cnt \n" +
            "FROM\n" +
            "	tbl_topic a\n" +
            "	JOIN tbl_user b ON a.uid = b.id\n" +
            "	LEFT JOIN ( SELECT count(*) reply_cnt, topicid FROM tbl_reply GROUP BY topicid ) c ON a.id = c.topicid\n" +
            "where boardid = #{bid}")
    Page<Topic> selectByBid(@Param("bid") int bid, Page<Topic> page);

    @Select("SELECT\n" +
            "	a.*,\n" +
            "	b.cname \"user.cname\",\n" +
            "	b.head \"user.head\"," +
            "	b.regtime \"user.regtime\"," +
            "	reply_cnt \n" +
            "FROM\n" +
            "	tbl_topic a\n" +
            "	JOIN tbl_user b ON a.uid = b.id\n" +
            "	LEFT JOIN ( SELECT count(*) reply_cnt, topicid FROM tbl_reply GROUP BY topicid ) c ON a.id = c.topicid\n" +
            "where a.id = #{bid}")
    Topic queryById(int id);

    Page<Topic> searchByField(@Param("bid") int bid, @Param("cname")String cname, @Param("title")String title, Page<Topic> page);

}
